# Maintainer: Dominika Liberda <ja@sdomi.pl>
# Contributor: Dominika Liberda <ja@sdomi.pl>
pkgname=terra
pkgver=1.0.6
pkgrel=1
pkgdesc="Low-level system programming language embedded in and meta-programmed by Lua."
url="https://terralang.org/"
# does not support new llvm versions, looks dead upstream
#arch="x86_64"
license="MIT"
makedepends="
	clang-dev
	clang-static
	cmake
	libedit-dev
	libffi-dev
	libxml2-dev
	lld
	llvm-dev
	llvm-static
	luajit-dev
	ncurses-dev
	openmp-dev
	samurai
	zlib-dev
	"
source="$pkgname-$pkgver.tar.gz::https://github.com/terralang/terra/archive/refs/tags/release-$pkgver.tar.gz
	no-execinfo.patch
	system-lua.patch
	version.patch
	"
subpackages="$pkgname-dev"
builddir="$srcdir/$pkgname-release-$pkgver"

prepare() {
	default_prepare

	# checks if puts() returns 10
	rm tests/string.t
	# includes immintrin.h without include path
	rm tests/expvec.t
	# ?
	rm tests/option_e.t
	rm tests/dynlib.t
}

build() {
	CC=clang \
	CXX=clang++ \
	CFLAGS="$CFLAGS -flto=thin" \
	CXXFLAGS="$CXXFLAGS -flto=thin" \
	LDFLAGS="$LDFLAGS -fuse-ld=lld" \
	cmake -G Ninja -B build \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_INSTALL_LIBDIR=lib \
		-DCMAKE_SKIP_INSTALL_RPATH=ON \
		-DBUILD_SHARED_LIBS=True \
		-DCMAKE_BUILD_TYPE=Release \
		-DTERRA_STATIC_LINK_LUAJIT=OFF \
		-DTERRA_VERSION="$pkgver"
	cmake --build build
}

check() {
	cd tests
	INCLUDE_PATH="$(pkg-config --variable=includedir luajit)" \
	../build/bin/terra run
}

package() {
	DESTDIR="$pkgdir" cmake --install build
	rm -rf "$pkgdir"/usr/share/terra/tests
}

sha512sums="
ba856b41533a5db4a68f8993fdc61164104d55e1cab46417176840b1c40867fb31889d1fad8996644debee2ca69f78d7b2a99a36004b236c958ecdb9d8beff98  terra-1.0.6.tar.gz
9bba656697bb25249a5657d87481c242cf68a3af10ef8d076aace31c9dd28c1bda77baecbc1d281aca627f2d47a75d379084cefc6c7d6bdcf74e391a0a24d54b  no-execinfo.patch
c5407edc1cdcd03d1ffe1a0db60f10f86e2223edd4d90e831285cc5b36dc687806ce6532196563a24c326868f446a32d976208a6398668e85d82d3fc57d77a2c  system-lua.patch
d2beea5e219951c1a2d157e67e1ba238bcd07f526a955fc84779477ad9b36b63b72d9ae402c6ce196f7ea4fccc503478a4ee9c77aa4bc22804434620362ab8a2  version.patch
"
